//
//  HYBNavigationController.m
//  EMMApp
//
//  Created by Mac on 16/5/11.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "HYBNavigationController.h"
#import "EMMBaseViewController.h"

@interface HYBNavigationController () <UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL enableRightGesture;
@property (nonatomic, strong) UIPanGestureRecognizer *popPanGesture;

@property (nonatomic, strong) id popGestureDelegate;
@end

@implementation HYBNavigationController


- (void)viewDidLoad {
  [super viewDidLoad];
  
    [self addFullScreenPopPanGesture];
//    self.delegate = self;
    
//    self.popGestureDelegate = self.interactivePopGestureRecognizer.delegate;
//    SEL action = NSSelectorFromString(@"handleNavigationTransition:");
//    self.popPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.popGestureDelegate action:action];
//    self.popPanGesture.maximumNumberOfTouches = 1;
//
//  self.enableRightGesture = YES;
//  self.interactivePopGestureRecognizer.delegate = self;
}

- (void)addFullScreenPopPanGesture{
    
     SEL action = NSSelectorFromString(@"handleNavigationTransition:");
    self.popPanGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:action];
    self.popPanGesture.delegate = self;
    [self.view addGestureRecognizer:self.popPanGesture];
    [self.interactivePopGestureRecognizer requireGestureRecognizerToFail:self.popPanGesture];
    self.interactivePopGestureRecognizer.enabled = NO;
}

#pragma mark -- UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isEqual:self.popPanGesture]) {
        //获取手指移动后的相对偏移量
        CGPoint translationPoint = [self.popPanGesture translationInView:self.view];
        //向右滑动 && 不是跟视图控制器
        if (translationPoint.x > 0 && self.childViewControllers.count > 1) {
            return YES;
        }
        return NO;
    }
    return YES;
    
}


//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
////  if ([self.topViewController isKindOfClass:[EMMBaseViewController class]]) {
////    if ([self.topViewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
////      EMMBaseViewController *vc = (EMMBaseViewController *)self.topViewController;
////      self.enableRightGesture = [vc gestureRecognizerShouldBegin];
////    }
////  }
//
//  return self.childViewControllers.count > 1;
//}
//
//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//  if ([viewController isKindOfClass:[EMMBaseViewController class]]) {
//    if ([viewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
//      EMMBaseViewController *vc = (EMMBaseViewController *)viewController;
//      self.enableRightGesture = [vc gestureRecognizerShouldBegin];
//    }
//  }
//
//  [super pushViewController:viewController animated:YES];
//}
//
//- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
//     self.enableRightGesture = YES;
//  return [super popToRootViewControllerAnimated:animated];
//}
//
//- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
//  if (self.viewControllers.count == 1) {
//    self.enableRightGesture = YES;
//  } else {
//    NSUInteger index = self.viewControllers.count - 2;
//    UIViewController *destinationController = [self.viewControllers objectAtIndex:index];
//    if ([destinationController isKindOfClass:[EMMBaseViewController class]]) {
//      if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
//        EMMBaseViewController *vc = (EMMBaseViewController *)destinationController;
//        self.enableRightGesture = [vc gestureRecognizerShouldBegin];
//      }
//    }
//  }
//
//  return [super popViewControllerAnimated:animated];
//}
//
//- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
//  if (self.viewControllers.count == 1) {
//    self.enableRightGesture = YES;
//  } else {
//    UIViewController *destinationController = viewController;
//    if ([destinationController isKindOfClass:[EMMBaseViewController class]]) {
//      if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
//        EMMBaseViewController *vc = (EMMBaseViewController *)destinationController;
//        self.enableRightGesture = [vc gestureRecognizerShouldBegin];
//      }
//    }
//  }
//
//  return [super popToViewController:viewController animated:animated];
//}
//#pragma mark - UINavigationControllerDelegate
//- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    //    [self setStatusBarBackgroundColor];
//    BOOL isRootVC = viewController == navigationController.viewControllers.firstObject;
//
//        if (isRootVC) {
//            [self.view removeGestureRecognizer:self.popPanGesture];
//        } else {
//            [self.view addGestureRecognizer:self.popPanGesture];
//        }
//        self.interactivePopGestureRecognizer.delegate = self.popGestureDelegate;
//        self.interactivePopGestureRecognizer.enabled = NO;
//
//
//}
//
//#pragma mark - UIGestureRecognizerDelegate
//
////修复有水平方向滚动的ScrollView时边缘返回手势失效的问题
//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//    return YES;
//}
//
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
//}

@end
